package com.bianguo.android.iocdemo.view.widget;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateInterpolator;

import com.bianguo.android.injectlibrary.utils.MLog;
import com.bianguo.android.iocdemo.R;

import java.util.Timer;
import java.util.TimerTask;

public class JumpView extends View {

    private final float MAX_HEIGHT = 80;
    private Paint mPaint, txtPaint;
    private AnimatorSet animator;
    private int mWidth, mHeight;
    private Path mPath, txtPath;
    private int currentShape;
    private Timer mTimer;

    public JumpView(Context context) {
        this(context, null);
    }

    public JumpView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public JumpView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        mPaint = new Paint();
        mPaint.setColor(Color.BLUE);
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);

        txtPaint = new Paint();
        txtPaint.setColor(Color.BLACK);
        txtPaint.setAntiAlias(true);
        txtPaint.setStyle(Paint.Style.STROKE);
        txtPaint.setStrokeWidth(2);

        mPath = new Path();
        txtPath = new Path();
        animator = new AnimatorSet();
        ObjectAnimator tranAnim = ObjectAnimator.ofFloat(this, "translationY", 0, MAX_HEIGHT);
        tranAnim.setRepeatCount(ValueAnimator.INFINITE);
        tranAnim.setRepeatMode(ValueAnimator.REVERSE);
        ObjectAnimator rotateAnim = ObjectAnimator.ofFloat(this, "rotation", 0, 360);
        rotateAnim.setRepeatCount(ValueAnimator.INFINITE);
        animator.playTogether(tranAnim, rotateAnim);
        animator.setDuration(500);
        animator.setInterpolator(new AccelerateInterpolator());
        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                startTimer();
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                stopTimer();
            }

            @Override
            public void onAnimationCancel(Animator animation) {
                stopTimer();
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
            }
        });
        animator.start();
    }

    private void startTimer() {
        if (mTimer == null) {
            mTimer = new Timer();
            mTimer.schedule(new TimerTask() {
                @Override
                public void run() {
                    currentShape++;
                    currentShape %= 3;
                    invalidate();
                }
            }, 0, 1000);
        }
    }

    private void stopTimer() {
        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
        }
    }

    public void stopAnim() {
        if (animator.isStarted()) {
            animator.cancel();
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = getDimension(widthMeasureSpec);
        mHeight = getDimension(heightMeasureSpec);
        setMeasuredDimension(mWidth, mHeight);
    }

    private int getDimension(int measureSpec) {
        int result = 0;
        int mode = MeasureSpec.getMode(measureSpec);
        switch (mode) {
            case MeasureSpec.AT_MOST:
                result = 20;
                break;
            case MeasureSpec.EXACTLY:
                result = MeasureSpec.getSize(measureSpec);
                break;
        }
        return result;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (0 == currentShape) {
            mPaint.setColor(ActivityCompat.getColor(getContext(), R.color.blue));
            canvas.drawCircle(mWidth / 2, mHeight / 2, mWidth / 2, mPaint);
        } else if (1 == currentShape) {
            mPaint.setColor(ActivityCompat.getColor(getContext(), R.color.green));
            canvas.drawRect(new RectF(0, 0, mWidth, mHeight), mPaint);
        } else {
            mPaint.setColor(ActivityCompat.getColor(getContext(), R.color.pink));
            drawHexagon(canvas);
        }
    }

    private void drawTriangle(Canvas canvas) {
        mPath.moveTo(mWidth / 2, 0);
        mPath.lineTo(0, mHeight);
        mPath.lineTo(mWidth, mHeight);
        mPath.close();
        canvas.drawPath(mPath, mPaint);
    }

    private void drawHexagon(Canvas canvas) {
        mPath.moveTo(mWidth / 5f, 0);
        mPath.lineTo(mWidth - mWidth / 5f, 0);
        mPath.lineTo(mWidth, mHeight / 2);
        mPath.lineTo(mWidth - mWidth / 5f, mHeight);
        mPath.lineTo(mWidth / 5f, mHeight);
        mPath.lineTo(0, mHeight / 2);
        mPath.close();
        canvas.drawPath(mPath, mPaint);
    }

    private void drawTxt(Canvas canvas) {
        txtPath.moveTo(0, mHeight);
//        txtPath.lineTo(mWidth, mHeight);
        txtPath.quadTo(mWidth / 2, 0, mWidth, mHeight);
        canvas.drawTextOnPath("正在加载...", txtPath, 20, 20, txtPaint);
    }
}
